<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="物资名称" prop="materialName" style="width: 200px">
        <el-input
          v-model="queryParams.materialName"
          placeholder="请输入物资名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物资编码" prop="materialCode" style="width: 210px">
        <el-input
          v-model="queryParams.materialCode"
          placeholder="请输入物资编码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="category" style="width: 210px">
        <el-input
          v-model="queryParams.category"
          placeholder="请输入类型"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="单位: kg, g, L, ml, 瓶, 袋等" prop="unit">
        <el-input
          v-model="queryParams.unit"
          placeholder="请输入单位: kg, g, L, ml, 瓶, 袋等"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="累计入库" prop="totalIn">
        <el-input
          v-model="queryParams.totalIn"
          placeholder="请输入累计入库"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="累计出库" prop="totalOut">
        <el-input
          v-model="queryParams.totalOut"
          placeholder="请输入累计出库"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="当前库存" prop="currentStock">
        <el-input
          v-model="queryParams.currentStock"
          placeholder="请输入当前库存"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="预警阈值" prop="threshold">
        <el-input
          v-model="queryParams.threshold"
          placeholder="请输入预警阈值"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="供应商" prop="supplier" style="width: 210px">
        <el-input
          v-model="queryParams.supplier"
          placeholder="请输入供应商"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="批次号" prop="batchNumber" style="width: 210px">
        <el-input
          v-model="queryParams.batchNumber"
          placeholder="请输入批次号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="生产日期" style="width: 308px">
        <el-date-picker
          v-model="daterangeProductionDate"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item> -->
      <el-form-item label="有效期" style="width: 230px">
        <el-date-picker
          v-model="daterangeExpiryDate"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['SysFeedingRecords:SysInventory:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['SysFeedingRecords:SysInventory:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['SysFeedingRecords:SysInventory:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['SysFeedingRecords:SysInventory:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="SysInventoryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="${comment}" align="center" prop="id" /> -->
      <el-table-column label="物资名称" align="center" prop="materialName" :show-overflow-tooltip="true"/>
      <el-table-column label="物资编码" align="center" prop="materialCode" />
      <el-table-column label="类型" align="center" prop="category" />
      <!-- <el-table-column label="单位: kg, g, L, ml, 瓶, 袋等" align="center" prop="unit" /> -->
      <el-table-column label="累计入库" align="center" prop="totalIn" />
      <el-table-column label="累计出库" align="center" prop="totalOut" />
      <el-table-column label="当前库存" align="center" prop="currentStock" />
      <el-table-column label="预警阈值" align="center" prop="threshold" />
      <el-table-column label="供应商" align="center" prop="supplier" />
      <el-table-column label="批次号" align="center" prop="batchNumber" />
      <!-- <el-table-column label="生产日期" align="center" prop="productionDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.productionDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="有效期" align="center" prop="expiryDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.expiryDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['SysFeedingRecords:SysInventory:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['SysFeedingRecords:SysInventory:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改物资库存对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="SysInventoryRef" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="物资名称" prop="materialName">
              <el-input v-model="form.materialName" placeholder="请输入物资名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类型" prop="category">
              <el-input v-model="form.category" placeholder="请输入类型" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="累计入库" prop="totalIn">
              <el-input v-model="form.totalIn" placeholder="请输入累计入库" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="累计出库" prop="totalOut">
              <el-input v-model="form.totalOut" placeholder="请输入累计出库" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="供应商" prop="supplier">
              <el-input v-model="form.supplier" placeholder="请输入供应商" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="批次号" prop="batchNumber">
              <el-input v-model="form.batchNumber" placeholder="请输入批次号" />
            </el-form-item>
          </el-col>
        </el-row>
<!--        <el-row :gutter="15">-->
<!--          <el-col :span="12">-->
<!--            <el-form-item label="当前库存" prop="currentStock">-->
<!--              <el-input v-model="form.currentStock" placeholder="请输入当前库存" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="12">-->
            <el-form-item label="物资编码" prop="materialCode">
              <el-input v-model="form.materialCode" placeholder="请输入物资编码" />
            </el-form-item>
<!--          </el-col>-->
<!--        </el-row>-->


        <!-- <el-form-item label="单位: kg, g, L, ml, 瓶, 袋等" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位: kg, g, L, ml, 瓶, 袋等" />
        </el-form-item> -->



        <el-form-item label="预警阈值" prop="threshold">
          <el-input v-model="form.threshold" placeholder="请输入预警阈值" />
        </el-form-item>


        <!-- <el-form-item label="生产日期" prop="productionDate">
          <el-date-picker clearable
            v-model="form.productionDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择生产日期">
          </el-date-picker>
        </el-form-item> -->
        <el-form-item label="有效期" prop="expiryDate">
          <el-date-picker clearable
            v-model="form.expiryDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择有效期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="SysInventory">
import { listSysInventory, getSysInventory, delSysInventory, addSysInventory, updateSysInventory } from "@/api/SysFeedingRecords/SysInventory"

const { proxy } = getCurrentInstance()

const SysInventoryList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")
const daterangeProductionDate = ref([])
const daterangeExpiryDate = ref([])

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    materialName: null,
    materialCode: null,
    category: null,
    unit: null,
    totalIn: null,
    totalOut: null,
    currentStock: null,
    threshold: null,
    supplier: null,
    batchNumber: null,
    productionDate: null,
    expiryDate: null,
    remark: null,
  },
  rules: {
    materialName: [
      { required: true, message: "物资名称不能为空", trigger: "blur" }
    ],
    category: [
      { required: true, message: "类型: feed, medicine, disinfectant, other不能为空", trigger: "blur" }
    ],
    unit: [
      { required: true, message: "单位: kg, g, L, ml, 瓶, 袋等不能为空", trigger: "blur" }
    ],
    totalIn: [
      { required: true, message: "累计入库不能为空", trigger: "blur" },
      { pattern: /^\d+$/, message: "累计入库请输入数字(整数)", trigger: "blur" }
    ],
    totalOut: [
      { required: true, message: "累计出库不能为空", trigger: "blur" },
      { pattern: /^\d+$/, message: "累计出库请输入数字(整数)", trigger: "blur" },
      {
        validator: (rule, value, callback) => {
          const totalIn = form.value.totalIn;
          if (value > totalIn) {
            callback(new Error("累计出库不能大于累计入库"));
          } else {
            callback();
          }
        },
        trigger: "blur"
      }
    ],
    currentStock: [
      { required: true, message: "当前库存不能为空", trigger: "blur" },
      { pattern: /^\d+$/, message: "当前库存请输入数字(整数)", trigger: "blur" }
    ],
    status: [
      { required: true, message: "状态: 1-正常, 0-停用不能为空", trigger: "change" }
    ],
    createTime: [
      { required: true, message: "$comment不能为空", trigger: "blur" }
    ],
    updateTime: [
      { required: true, message: "$comment不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询物资库存列表 */
function getList() {
  loading.value = true
  queryParams.value.params = {}
  if (null != daterangeProductionDate && '' != daterangeProductionDate) {
    queryParams.value.params["beginProductionDate"] = daterangeProductionDate.value[0]
    queryParams.value.params["endProductionDate"] = daterangeProductionDate.value[1]
  }
  if (null != daterangeExpiryDate && '' != daterangeExpiryDate) {
    queryParams.value.params["beginExpiryDate"] = daterangeExpiryDate.value[0]
    queryParams.value.params["endExpiryDate"] = daterangeExpiryDate.value[1]
  }
  listSysInventory(queryParams.value).then(response => {
    SysInventoryList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    materialName: null,
    materialCode: null,
    category: null,
    unit: null,
    totalIn: null,
    totalOut: null,
    currentStock: null,
    threshold: null,
    supplier: null,
    batchNumber: null,
    productionDate: null,
    expiryDate: null,
    status: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  }
  proxy.resetForm("SysInventoryRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeProductionDate.value = []
  daterangeExpiryDate.value = []
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加物资库存"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getSysInventory(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改物资库存"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["SysInventoryRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateSysInventory(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addSysInventory(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除物资库存编号为"' + _ids + '"的数据项？').then(function() {
    return delSysInventory(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('SysFeedingRecords/SysInventory/export', {
    ...queryParams.value
  }, `SysInventory_${new Date().getTime()}.xlsx`)
}

getList()
</script>
